package com.sys.web.back.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sys.common.pojo.ApiResponse;
import com.sys.common.servlet.BaseServlet;
import com.sys.common.util.ConstatFinalUtil;
import com.sys.common.util.PageInfoUtil;
import com.sys.users.pojo.TUsers;
import com.sys.users.service.IUsersDbService;
import com.sys.users.service.impl.UsersDbServiceImpl;


/**
 * 	管理员首页操作
 * 	@author 杨志超
 */
@WebServlet(urlPatterns = "/back/UsersBackServlet")
public class UsersBackServlet extends BaseServlet
{
	private IUsersDbService usersDbService;
	
	@Override
	public void init(ServletConfig config) throws ServletException
	{
		super.init(config);
		this.usersDbService = new UsersDbServiceImpl();
	}
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 接收参数 */
		String method = request.getParameter("method");
		if ("usersList".equalsIgnoreCase(method))
		{
			/* usersList */
			usersList(request, response);
		} else if ("usersInsert".equalsIgnoreCase(method))
		{
			/* usersInsert */
			usersInsert(request, response);
		} else if ("usersInsertSubmit".equalsIgnoreCase(method))
		{
			/* usersInsertSubmit */
			usersInsertSubmit(request, response);
		} else if ("usersUpdate".equalsIgnoreCase(method))
		{
			/* usersUpdate */
			usersUpdate(request, response);
		} else if ("usersUpdateSubmit".equalsIgnoreCase(method))
		{
			/* usersUpdateSubmit */
			usersUpdateSubmit(request, response);
		} 
	}

	/**
	 * 用户列表
	 * @return
	 * @throws IOException 
	 * @throws ServletException 
	 */
	public void usersList(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
	{
		ConstatFinalUtil.SYS_LOGGER.info("--usersList--");
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		
		/*-------自定义搜索条件-------*/
		String userType = request.getParameter("userType");
		if(userType == null)
		{
			userType = "" ;
		}
		condMap.put("userType", userType);
		request.setAttribute("userType", userType);
		
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<TUsers> apiResponse = this.usersDbService.findCondListUsersService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		String path = this.prefix + "/back/users/usersList" + this.subfix; 
		request.getRequestDispatcher(path).forward(request, response);
	}
	
	/**
	 * 打开添加用户页面
	 * @return
	 * @throws IOException 
	 * @throws ServletException 
	 */
	public void usersInsert(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
	{
		ConstatFinalUtil.SYS_LOGGER.info("--usersInsert--");
		String path = this.prefix + "/back/users/usersInsert" + this.subfix; 
		request.getRequestDispatcher(path).forward(request, response);
	}
	
	/**
	 * 添加用户提交操作
	 * @return
	 */
	public void usersInsertSubmit(HttpServletRequest request,HttpServletResponse response)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--usersInsertSubmit--");
		String birthdayStr = request.getParameter("birthdayStr");
		
		TUsers users = new TUsers();
		/* 为对象赋值 */
		users.setBirthday(this.dateFormatUtil.strDate(birthdayStr));
		/* 为对象赋值 */
		/* 保存结果 */
		ApiResponse apiResponse = this.usersDbService.saveOneUsersService(users);
		ConstatFinalUtil.SYS_LOGGER.info("--用户添加返回结果:{}--",apiResponse.toJSON());
		this.printOut(apiResponse.toJSON().toJSONString(), response);
	}
	
	/**
	 * 用户查看
	 * @return
	 * @throws IOException 
	 * @throws ServletException 
	 */
	public void usersUpdate(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
	{
		String path = "/back/users/usersInfo" ; 
		ConstatFinalUtil.SYS_LOGGER.info("--usersInfo--");
		String id = request.getParameter("id");
		/* 准备查询条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", id);
		/* 查询数据库 */
		ApiResponse<TUsers> apiResponse = this.usersDbService.findOneUsersService(condMap);
		/* 存储request */
		request.setAttribute("response", apiResponse.toJSON());
		
		String operType = request.getParameter("operType");
		request.setAttribute("operType", operType);
		if("update".equalsIgnoreCase(operType))
		{
			path = "/back/users/usersUpdate";
		}
		
		path = this.prefix + path + this.subfix; 
		request.getRequestDispatcher(path).forward(request, response);
	}
	
	/**
	 * 添加用户提交操作
	 * @return
	 */
	public void usersUpdateSubmit(HttpServletRequest request,HttpServletResponse response)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--usersInsertSubmit--");
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		/* 准备查询条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		String id = request.getParameter("id");
		if(id == null)
		{
			id = "" ; 
		}
		condMap.put("id", id);
		/* 查询数据库 */
		ApiResponse<TUsers> usersApiResponse = this.usersDbService.findOneUsersService(condMap);
		TUsers users = usersApiResponse.getDataOneJava() ; 
		/* 存储request */
		request.setAttribute("response", usersApiResponse.toJSON());
		
		String operType = request.getParameter("operType");
		request.setAttribute("operType", operType);
		if("update".equalsIgnoreCase(operType))
		{
			String status = request.getParameter("status");
			String userType = request.getParameter("userType");
			String discount = request.getParameter("discount");
			String birthdayStr = request.getParameter("birthdayStr");
			
			users.setStatus(Byte.valueOf(status));
			users.setUserType(Byte.valueOf(userType));
			users.setDiscount(Double.valueOf(discount));
			users.setBirthday(this.dateFormatUtil.strDate(birthdayStr));
			
			/* 为对象赋值 */
			users.setBirthday(this.dateFormatUtil.strDate(birthdayStr));
			
			users.setUpdateTime(new Date());
			
			apiResponse = this.usersDbService.updateOneUsersService(users);
		}
		
		this.printOut(apiResponse.toJSON().toJSONString(), response);
	}
}